<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="margin-bottom">
      <!-- 搜索 -->
      <el-input
        clearable
        placeholder="输入字典名称搜索"
        style="width: 200px;"
        size="mini"
        class="filter-item"
      />
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-search">搜索</el-button>
      <!-- 新增 -->
      <div style="float:right">
        <el-button
          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-plus"
          @click="add"
        >新增</el-button>
      </div>
    </div>
    <!--表单组件-->
    <eForm ref="form" :is-add="isAdd" :title="title"/>
    <!--表格渲染-->
    <el-table
      v-loading="loading"
      :data="data"
      border
      :expand-row-keys="['1']"
      row-key="id"
      style="width: 100%;"
    >
      <el-table-column prop="supplement" label="字典名称" align="center"/>
      <el-table-column prop="name" label="字典标签" align="center"/>
      <el-table-column :show-overflow-tooltip="true" prop="value" label="字典值" align="center"/>
      <el-table-column label="操作" align="center" width="300">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-plus" @click="append(scope.row)"/>
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            style="margin:0"
            @click="edit(scope.row)"
          />
          <el-popover v-if="!scope.row.children" :ref="scope.row.id" placement="top" width="200">
            <p>确定删除该节点吗?</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
              <el-button
                :loading="delLoading"
                type="primary"
                size="mini"
                @click="remove(scope.row.id)"
              >确定</el-button>
            </div>
            <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini"/>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import eForm from './dic-form';
import { dictionary_all, dictionary_remove } from '@/api/system';
import Menu from '@/utils/menu';
export default {
  components: { eForm },
  data() {
    return {
      data: [],
      loading: false,
      delLoading: false,
      isAdd: false,
      title: ''
    };
  },
  created() {
    this.getData();
  },
  methods: {
    async getData() {
      this.loading = true;
      const res = await dictionary_all();

      this.data = Menu.convertDicTree(res.data);
      this.loading = false;
    },
    add() {
      this.isAdd = true;
      const _this = this.$refs.form;
      _this.resetForm();
      _this.dialog = true;
    },
    append(data) {
      this.isAdd = true;
      const _this = this.$refs.form;
      _this.resetForm();
      _this.dialog = true;
      _this.form.parentId = data.id;
    },
    edit(data) {
      this.isAdd = false;
      const _this = this.$refs.form;
      _this.form = {
        id: data.id,
        lv: data.lv,
        name: data.name,
        value: data.value,
        supplement: data.supplement,
        parentId: data.parentId
      };
      _this.dialog = true;
    },
    async remove(id) {
      try {
        this.delLoading = true;
        await dictionary_remove(id);
        this.$refs[id].doClose();
        this.$message({
          message: '删除成功',
          type: 'success'
        });
        this.delLoading = false;
        this.getData();
      } catch (error) {
        this.delLoading = false;
        throw error;
      }
    }
  }
};
</script>
